<?php


namespace app\controller\api;


use app\BaseController;
use app\models\user\UserAddressModel;
use app\services\area\RegionService;
use app\validate\UserAddressValidate;

class AddressController extends BaseController
{
    /**
     * 获取用户默认地址和运费
     * @return \think\response\Json
     */
    public function getDefaultAddress()
    {
        try {
            $userId = $this->request->userId;
            $info = UserAddressModel::where('user_id', $userId)->where('status', 0)->where('is_default', 1)->find();
            return $this->success($info);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 获取用户地址列表
     * @return \think\response\Json
     */
    public function getAddressList()
    {
        try {
            $userId = $this->request->userId;
            $userInfo = $this->request->userInfo;
            $list = UserAddressModel::where('user_id', $userId)->where('status', 0)->order('is_default', 'desc')->select();
            foreach ($list as &$value) {
                $value['avatar'] = $userInfo['avatar'];
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 设置地址
     * @return \think\response\Json
     */
    public function setAddress()
    {
        try {
            $userId = $this->request->userId;
            $id = $this->request->param('id');

            $data = $this->request->only(['username', 'mobile', 'province_id', 'city_id', 'area_id', 'address', 'is_default']);
            //Validate(UserAddressValidate::class)->check($data);

            $data['user_id'] = $userId;
            $address = RegionService::getAddress($data['province_id'], $data['city_id'], $data['area_id']);
            $data['full_address'] = $address . $data['address'];

            if ($data['is_default'] == 1) {
                UserAddressModel::where('user_id', $userId)->update(['is_default' => 0]);
            }

            if (!$id) {
                $model = new UserAddressModel();
                $model->save($data);
            } else {
                $model = UserAddressModel::find($id);
                $model->save($data);
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 删除地址
     * @return \think\response\Json
     */
    public function delAddress()
    {
        try {
            $userId = $this->request->userId;
            $id = $this->request->param('id');
            $model = UserAddressModel::find($id);
            if (!$model || $model->user_id != $userId) {
                throw new \Exception('非法请求');
            }
            $model->status = 1;
            $model->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 默认地址
     * @return \think\response\Json
     */
    public function setDefaultAddress()
    {
        try {
            $userId = $this->request->userId;
            $id = $this->request->param('id');

            $model = UserAddressModel::find($id);
            if (!$model || $model->user_id != $userId) {
                throw new \Exception('非法请求');
            }
            if ($model['is_default'] == 1) {
                return $this->success();
            }
            UserAddressModel::where('user_id', $userId)->update(['is_default' => 0]);
            $model->is_default = 1;
            $model->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}